package org.tlipoca.smcomment.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.genntii.smcommon.constant.GatewayConstant;
import org.genntii.smcommon.result.PageResult;
import org.genntii.smcommon.result.Result;
import org.genntii.smdomain.dto.CommentDTO;
import org.genntii.smdomain.entity.Comment;
import org.genntii.smdomain.vo.CommentClientVO;
import org.genntii.smdomain.vo.CommentDetailVO;
import org.genntii.smdomain.vo.CommentReplyVO;
import org.genntii.smdomain.vo.CommentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.tlipoca.smcomment.service.CommentService;

import java.util.ArrayList;
import java.util.Map;

@Tag(name = "评论")
@Slf4j
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @GetMapping("/userTotal/{userId}")
    public Result<Long> getTotal(@PathVariable Long userId){
        return Result.success(commentService.getCommentTotalByUserId(userId));
    }

    @Operation(summary = "根据articleId获取文章评论数")
    @GetMapping("commentCount")
    public Result<Integer> getCommentCount(@RequestParam("articleId") Long articleId){
        return Result.success(commentService.getCommentCount(articleId));
    }

    @Operation(summary = "评论是否存在")
    @GetMapping("/exists")
    public Result<Boolean> getCommentExists(@RequestParam(name = "commentId") Long commentId) {

        return Result.success(commentService.getCommentExistsService(commentId));
    }

    @Operation(summary = "获取评论全部评论")
    @GetMapping("getReply")
    public Result<ArrayList<CommentReplyVO>> getAllReply(@RequestParam("commentId") Long commentId, HttpServletRequest request){
        Long userId = Long.valueOf(request.getHeader(GatewayConstant.USER_ID));
        return Result.success(commentService.getAllReply(commentId,userId));
    }

    @Operation(summary = "根据文章ID获取评论列表")
    @GetMapping("/{articleId}")
    public Result<PageResult<CommentDetailVO>> getCommentListByArticleId(@PathVariable Long articleId,
                                                                         @RequestParam Integer pageNum,
                                                                         @RequestParam Integer pageSize,
                                                                         HttpServletRequest request) {
        Long userId = Long.valueOf(request.getHeader(GatewayConstant.USER_ID));
        return Result.success(commentService.getCommentListByArticleIdService(articleId,pageNum,pageSize,userId));
    }

    @Operation(summary = "根据文章用户ID获取回复树")
    @GetMapping("/article/{userId}")
    public Result<ArrayList<CommentVO>> getCommentTree(@RequestParam(name = "articleId") Long articleId, @PathVariable Long userId) {
        return Result.success(commentService.getCommentTreeService(articleId, userId));
    }

    @Operation(summary = "新增评论")
    @PostMapping
    public Result addComment(@RequestBody CommentDTO commentDTO,
                             HttpServletRequest request) {
        Long userId = Long.valueOf(request.getHeader(GatewayConstant.USER_ID));
        commentService.addCommentService(commentDTO,userId,commentDTO.getReplyCommentId());
        return Result.success();
    }

    @Operation(summary = "删除评论")
    @DeleteMapping("/{commentId}")
    public Result deleteComment(@PathVariable Long commentId) {
        commentService.deleteComment(commentId);
        return Result.success();
    }

    @Operation(summary = "批量上传图片")
    @PostMapping("/picBatchUpload")
    public Result<Map<Integer, String>> picBatchUpload(@RequestPart("files") MultipartFile[] files){
        return Result.success(commentService.picBatchUpload(files));
    }

    @Operation(summary = "上传图片")
    @PostMapping("/picUpload")
    public Result<String> picUpload(@RequestPart("file") MultipartFile file){
        return Result.success(commentService.picUpload(file));
    }


    @GetMapping("/getByUser/{userId}")
    public Result<ArrayList<CommentClientVO>> getCommentByUserId(@PathVariable Long userId){
        return Result.success(commentService.getArticleByUserId(userId));
    }

    @GetMapping("/commentLimit")
    Result<ArrayList<CommentClientVO>> getCommentLimit(@RequestParam("userId")Long userId,
                                                       @RequestParam("pageNum") Integer pageNum,
                                                       @RequestParam("pageSize") Integer pageSize){

        return Result.success(commentService.getCommentLimit(userId,pageNum,pageSize));
    }

    @Operation(summary = "单条评论")
    @GetMapping("/oneCommentDetail/{commentId}")
    public Result<CommentDetailVO> getOneCommentDetailVO(@PathVariable Long commentId,
                                                         HttpServletRequest request){
        Long userId = Long.valueOf(request.getHeader(GatewayConstant.USER_ID));
        return Result.success(commentService.getCommentDetail(commentId,userId));
    }

    @Operation(summary = "单条回复评论")
    @GetMapping("/oneCommentReply/{commentId}")
    public Result<CommentReplyVO> getOneCommentReplyVO(@PathVariable Long commentId,
                                                       HttpServletRequest request){
        Long userId = Long.valueOf(request.getHeader(GatewayConstant.USER_ID));
        return Result.success(commentService.getCommentReply(commentId,userId));
    }


}
